﻿using SqlSugar;
using Newtonsoft.Json;
using Net6Shell.Dto;
using Net6Shell.DataModel;
using Net6Shell.IRepository;
using Net6Shell.Common;
using Net6Shell.Vo;

namespace MyApp.Repository
{
    public class InterfaceToolRepository : BaseRepository<InterfaceToolEntity>, IInterfaceToolRepository
    {
        #region 变量

        private readonly string deleteInterfaceToolSql;
        private readonly string insertInterfaceToolSql;
        private readonly string updateInterfaceToolSql;
        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSugarScope"></param>
        /// <exception cref="Exception"></exception>
        public InterfaceToolRepository(SqlSugarScope sqlSugarScope) : base(sqlSugarScope)
        {
            var jsonFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JsonSql", "InterfaceTool.json");
            NLogHelper.GetLog().Info("jsonsql地址" + jsonFile);
            if (!File.Exists(jsonFile))
                return;
            var orderJsonSqls = JsonConvert.DeserializeObject<List<JsonSql>>(File.ReadAllText(jsonFile));
            if (orderJsonSqls.Any())
            {
                deleteInterfaceToolSql = orderJsonSqls.FirstOrDefault(a => a.SqlKey.Equals("deleteInterfaceToolSql"))?.SqlValue;
                insertInterfaceToolSql = orderJsonSqls.FirstOrDefault(a => a.SqlKey.Equals("insertInterfaceToolSql"))?.SqlValue;
                updateInterfaceToolSql = orderJsonSqls.FirstOrDefault(a => a.SqlKey.Equals("updateInterfaceToolSql"))?.SqlValue;
            }
            else
            {
                throw new Exception("InterfaceTool.json语句文件加载出错！");
            }
        }
     


        /// <summary>
        /// 通过分页查询接口工具
        /// </summary>
        /// <param name="pageVm"></param>
        /// <returns></returns>
        public RPage<InterfaceToolEntity> GetInterfacePageList(PageVm<QueryInterFace> pageVm)
        {
            RPage<InterfaceToolEntity> res = new RPage<InterfaceToolEntity>();
            if (pageVm != null)
            {
                int totalCount = 0;
                var page = db.Queryable<InterfaceToolEntity>().ToPageList(pageVm.PageNumber, pageVm.PageSize, ref totalCount);
                res.TotalCount = totalCount;
                res.Data = page;
            }
            return res;
        }

        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="interfaces"></param>
        /// <returns></returns>
        public CommonResult SaveInterfaceTool(List<InterfaceToolEntity> interfaces)
        {
            var result = new CommonResult();

            try
            {

                db.Ado.BeginTran();
                try
                {
                    interfaces.ForEach(a =>
                    {

                        if (a.ISDELETE == 1)
                        {
                            db.Ado.ExecuteCommand(deleteInterfaceToolSql, new { a.ID });
                        }
                        else if (!string.IsNullOrEmpty(a.ID))
                        {
                            SugarParameter[] param = new SugarParameter[]{
                                    new SugarParameter(":INTERFACECODE",a.INTERFACECODE),
                                    new SugarParameter(":INTERFACENAME" , a.INTERFACENAME),
                                    new SugarParameter(":INTERFACEDESCRIPTION", a.INTERFACEDESCRIPTION),
                                    new SugarParameter(":INJSON", a.INJSON),
                                    new SugarParameter(":JAVASCRIPTCODE", a.JAVASCRIPTCODE),
                                    new SugarParameter(":OUTJSON", a.OUTJSON),
                                    new SugarParameter(":CLASSNAME", a.CLASSNAME),
                                    new SugarParameter(":FUNCTIONNAME", a.FUNCTIONNAME),
                                    new SugarParameter(":ID", a.ID),
                                    new SugarParameter(":CREATEDATE", a.CREATEDATE),
                                    new SugarParameter(":CREATEUSER", a.CREATEUSER),
                                    new SugarParameter(":UPDATEDATE", a.UPDATEDATE),
                                    new SugarParameter(":UPDATEUSER", a.UPDATEUSER),
                                    new SugarParameter(":SCRIPTTYPE", a.SCRIPTTYPE),
                                    new SugarParameter(":INVALIDFLAG", a.INVALIDFLAG),
                            };
                            db.Ado.ExecuteCommand(updateInterfaceToolSql, param);
                        }
                        else if (string.IsNullOrEmpty(a.ID))
                        {
                            a.ID = Guid.NewGuid().ToString();
                            a.INVALIDFLAG = 0;
                            SugarParameter[] param = new SugarParameter[]{
                                    new SugarParameter(":INTERFACECODE",a.INTERFACECODE),
                                    new SugarParameter(":INTERFACENAME" , a.INTERFACENAME),
                                    new SugarParameter(":INTERFACEDESCRIPTION", a.INTERFACEDESCRIPTION),
                                    new SugarParameter(":INJSON", a.INJSON),
                                    new SugarParameter(":JAVASCRIPTCODE", a.JAVASCRIPTCODE),
                                    new SugarParameter(":OUTJSON", a.OUTJSON),
                                    new SugarParameter(":CLASSNAME", a.CLASSNAME),
                                    new SugarParameter(":FUNCTIONNAME", a.FUNCTIONNAME),
                                    new SugarParameter(":ID", a.ID),
                                    new SugarParameter(":ISDELETE", a.ISDELETE),
                                    new SugarParameter(":CREATEDATE", a.CREATEDATE),
                                    new SugarParameter(":CREATEUSER", a.CREATEUSER),
                                    new SugarParameter(":UPDATEDATE", a.UPDATEDATE),
                                    new SugarParameter(":UPDATEUSER", a.UPDATEUSER),
                                    new SugarParameter(":SCRIPTTYPE", a.SCRIPTTYPE),
                                    new SugarParameter(":INVALIDFLAG", a.INVALIDFLAG),
                            };
                            db.Ado.ExecuteCommand(insertInterfaceToolSql, param);
                        }
                    });

                    db.Ado.CommitTran();
                }
                catch (Exception ex)
                {
                    db.Ado.RollbackTran();
                    result.Result = false;
                    result.ErrMsg = ex.Message;

                }

            }
            catch (Exception ex)
            {
                result.Result = false;
                result.ErrMsg = ex.Message;

            }
            return result;
        }

    }
}

